/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "bizProto","btnServ"],
		function($,datagridServ, msgServ, ajaxServ, formServ, bizProto,btnServ) {

			'use strict';

			var _bizProto,_buttonConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,
				
				//流程定义分页查询url
				$qryUrl = '/flowDefinition/getLastVersionListDomain.action',
				
				//流程定义未分页查询url
				$qryAllUrl = '/flowDefinition/getLastVersionListDomain.action',
				
				
				//流程发布url
				$deployUrl = '',
				
				//弹出窗口url
				$diagramUrl = '/flowDefinition/getDiagram.action',
				
				//上传附件url
				$uploadUrl = '/upload/mainPage.action',
				$clickedRow
				
			var public_method = {				
				
				init:function(doc){
					private_method.init(doc);
				}
				
			}

			var private_method = {
				init : function(doc) {
					if (window.console) {
					console.info('workflowDefinition modules gets ready and been loaded.');
					}
					_bizProto = new bizProto({doc:doc,rootId:'panel-workflowDefinition',panelSelector:'div.well',subSystemCode:'workflow',moduleCode:'flowDefinition'})
					_buttonConfig={
						"上传":private_method.add						 								
					}
					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
				},
				
				//初始化路径
				initPath:function(){
					$deployUrl = _bizProto.basePath + '/deploy.action';
//					$diagramUrl = _bizProto.rootPath + '/common/diagram-viewer/index.html';
					
				},				
				
				//初始化元素
				initElement:function(doc){
					
					
					$dg = $(doc).find("#workflowDefinition-dg");
					$grid = $dg.datagrid({
						height:_bizProto.pageHeight,
						url : _bizProto.rootPath + $qryUrl,
						width : 'auto',
						
						onClickCell:private_method.cellClick
						
					});
					
//					$grid.datagrid('load')

					// 绑定按钮事件
					var btn = $(doc).find('#tb-workflowDefinition a.function-btn')
					$(btn).unbind('click');
					$(btn).bind('click', function(e) {
						private_method.function_btn_listenner(e)
					})

					// 编辑窗口初始化
					$edit_window = $('#workflowDefinition-win')
					window_btn = $($edit_window).find('a.edit-window-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					$form = $($edit_window).find('form')	
					
					$.messager.progress('close');
					
				},				
				
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				// 新增函数
				add : function() {
					$form = $($edit_window).find('form')
					$($form).form('clear');
					var winHref = _bizProto.rootPath+ $uploadUrl;
					$edit_window.window({width:600,height:400,href:winHref,modal:true,closeable:true});
					$edit_window.window('open');
				},
				// 编辑,flag 为form按钮可见性标志
				edit : function(flag) {					
					$edit_window.window({width:800,height:600});
					$edit_window.window('open');
				},
				
				
				
				
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				
				cellClick:function(index,field,value){
					
					$clickedRow = $($grid).datagrid('getRows')[index];
					if($clickedRow){
						var imgUrl = _bizProto.rootPath + $diagramUrl +'?processDefinitionId='+$clickedRow.id
						var _param = {width:600,height:400,href:imgUrl}
//						$($edit_window).window(_param)
//						$($edit_window).window('open');
						window.open(imgUrl);
					}
					
				},
				
				rowClick:function(index,row){
					$clickedRow = row
				}

			};

			
			return public_method;

		});